const express = require("express");
const morgan = require("morgan");
const cors = require("cors");
const router = require("./router");
const errorHandler = require("./middleware/error-handler");

const app = express();
// morgan 日志中间件
app.use(morgan("dev"));
// json 解析
app.use(express.json());
// x-www-form-urlencoded 解析
app.use(express.urlencoded());
// 跨域 cors
app.use(cors());

const PORT = process.env.PORT || 80;

// 挂载路由
app.use("/api", router);

// 错误处理
app.use(errorHandler());

app.listen(PORT, "0.0.0.0", () => {
  console.log(`server is running at http://localhost:${PORT}`);
});
